#!/bin/bash

echo "Executing script by: chenguiming 24055060324"

MY_GENDER="M"

SOURCE_DIR="students"

if [ ! -d "$SOURCE_DIR" ]; then

    echo "Error: Directory '$SOURCE_DIR' not found!"

    exit 1

fi

cd "$SOURCE_DIR"

total_average_score=0.0

student_count=0



if [ "$MY_GENDER" = "M" ]; then

    gender_full_name="male(M)"

else

    gender_full_name="female(F)"

fi

echo "Calculating average score for all '$MY_GENDER' students..."



for file in *.txt; do

    gender=$(grep "gender:" "$file" | cut -d' ' -f2)

    

    if [ "$gender" = "$MY_GENDER" ]; then

        avg=$(grep "average:" "$file" | cut -d' ' -f2)

        total_average_score=$(echo "scale=2; $total_average_score + $avg" | bc)

        student_count=$((student_count + 1))

    fi

done

final_average=$(echo "scale=1; $total_average_score / $student_count" | bc)

echo "Total students found: $student_count"

echo "Average score of $gender_full_name: $final_average"


